package com.alstudio.utils.android.net.http.http2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;

import android.net.http.AndroidHttpClient;

import com.alstudio.utils.android.ALFileManager;
import com.alstudio.utils.android.net.http.http2.ProgressFileEntity.ProgressListener;

public class HttpUploadRequest {

	public static final String TAG = HttpUploadRequest.class.getSimpleName();

	private String url;
	private String filepath;
	private long fileSize = 0;
	private HttpParameters params = null;
	private HttpParameters headers = null;
	private AndroidHttpClient client;
	private HttpPost postMethod;
	private boolean isInterrupted = false;

	public HttpUploadRequest(String url, String filePath) {
		this.url = url;
		this.filepath = filePath;
	}

	public void addHeader(String name, String value) {
		if (headers == null)
			headers = new HttpParameters();

		headers.add(name, value);
	}

	public void addParameter(String name, String value) {
		if (params == null)
			params = new HttpParameters();

		params.add(name, value);
	}

	public void request(final IHttpProgressHandler handler) {
		request(handler, true);
	}

	public void request(final IHttpProgressHandler handler,
			boolean needProgressReport) {
		File file = new File(filepath);
		if (!file.exists()) {
			if (handler != null) {
				FileNotFoundException e = new FileNotFoundException();
				handler.handleException(e);
			}
			return;
		}

		ProgressListener progressListener = null;
		if (needProgressReport) {
			progressListener = new ProgressListener() {

				@Override
				public void transferred(long num) {
					if (handler != null)
						handler.handleProgress(num, fileSize);
				}
			};
		}

		fileSize = ALFileManager.getFileSize(filepath);

		postMethod = new HttpPost(url);
		ProgressFileEntity entity = new ProgressFileEntity(new File(filepath),
				"", progressListener);
		postMethod.setEntity(entity);

		if (headers != null) {
			for (int i = 0; i < headers.size(); ++i) {
				postMethod.setHeader(headers.getKey(i), headers.getValue(i));
			}
		}

		client = AndroidHttpClient.newInstance("");

		try {
			HttpResponse response = client.execute(postMethod);

			if (handler != null) {
				handler.handleResponse(response);
			}

		} catch (IOException e) {
			if (handler != null) {
				handler.handleException(e);
			}

		} finally {
			client.close();
		}

	}

	public void requestAsync(final IHttpProgressHandler handler) {
		requestAsync(handler, true);
	}

	public void requestAsync(final IHttpProgressHandler handler,
			final boolean needProgressReport) {
		new Thread() {

			@Override
			public void run() {
				request(handler, needProgressReport);
			}

		}.start();
	}

	public void cancel() {
		if (isInterrupted)
			return;

		isInterrupted = true;

		if (postMethod != null)
			postMethod.abort();

		if (client != null)
			client.close();
	}

}
